package dao

import (
	"time"

	"github.com/didi/gendry/scanner"
)

//UserOperate user_operate struct
type UserOperate struct {
	Id         int       `gorm:"id;pk" json:"id" default:""`
	Name       string    `gorm:"name" json:"name" default:""`
	CreateTime time.Time `gorm:"create_time" json:"create_time" default:""`
	IsDelete   int       `gorm:"is_delete" json:"is_delete" default:"0"`
	ModifyTime time.Time `gorm:"modify_time" json:"modify_time" default:""`
}

// TableName sets the insert table name for this struct type
func (b UserOperate) TableName() string {
	return "user_operate"
}

// UserOperates get user_operate list
func UserOperates(qs *Qs, other Other) (data []UserOperate, err error) {
	wh, params := qs.Format()
	others := other.Format()
	rows, err := Query(`
		select a.* 
		from user_operate a 
		`+wh+others, params...)
	if err != nil {
		return
	}
	err = scanner.ScanClose(rows, &data)
	return
}

// UserOperate get user_operate
func UserOperateo(qs *Qs) (data UserOperate, err error) {
	datas, err := UserOperates(qs, DefaultOther)
	if err != nil {
		return
	}
	if len(datas) == 0 {
		err = NotFound
		return
	}
	data = datas[0]
	return
}

//UserOperateRecord user_operate_record struct
type UserOperateRecord struct {
	Id                 int       `gorm:"id;pk" json:"id" default:""`
	UserOperateId      int       `gorm:"user_operate_id" json:"user_operate_id" default:""`
	UserId             int       `gorm:"user_id" json:"user_id" default:""`
	UserOperateTableId int       `gorm:"user_operate_table_id" json:"user_operate_table_id" default:""`
	CreateTime         time.Time `gorm:"create_time" json:"create_time" default:""`
	Remark             string    `gorm:"remark" json:"remark" default:""`
	DataId             string    `gorm:"data_id" json:"data_id" default:""`
	ModifyTime         time.Time `gorm:"modify_time" json:"modify_time" default:""`
	// other
	Username string `json:"username"`
}

// TableName sets the insert table name for this struct type
func (b UserOperateRecord) TableName() string {
	return "user_operate_record"
}

// UserOperateRecords get user_operate_record list
func UserOperateRecords(qs *Qs, other Other) (data []UserOperateRecord, err error) {
	wh, params := qs.Format()
	others := other.Format()
	rows, err := Query(`
		select a.*,b.username 
		from user_operate_record a 
		left join user b on a.user_id=b.id 
		`+wh+others, params...)
	if err != nil {
		return
	}
	err = scanner.ScanClose(rows, &data)
	return
}

// UserOperateRecord get user_operate_record
func UserOperateRecordo(qs *Qs) (data UserOperateRecord, err error) {
	datas, err := UserOperateRecords(qs, DefaultOther)
	if err != nil {
		return
	}
	if len(datas) == 0 {
		err = NotFound
		return
	}
	data = datas[0]
	return
}

//UserOperateTable user_operate_table struct
type UserOperateTable struct {
	Id         int       `gorm:"id;pk" json:"id" default:""`
	Name       string    `gorm:"name" json:"name" default:""`
	TName      string    `gorm:"t_name" json:"t_name" default:""`
	CreateTime time.Time `gorm:"create_time" json:"create_time" default:""`
	ModifyTime time.Time `gorm:"modify_time" json:"modify_time" default:""`
	IsDelete   int       `gorm:"is_delete" json:"is_delete" default:"0"`
}

// TableName sets the insert table name for this struct type
func (b UserOperateTable) TableName() string {
	return "user_operate_table"
}

// UserOperateTables get user_operate_table list
func UserOperateTables(qs *Qs, other Other) (data []UserOperateTable, err error) {
	wh, params := qs.Format()
	others := other.Format()
	rows, err := Query(`
		select a.* 
		from user_operate_table a 
		`+wh+others, params...)
	if err != nil {
		return
	}
	err = scanner.ScanClose(rows, &data)
	return
}

// UserOperateTable get user_operate_table
func UserOperateTableo(qs *Qs) (data UserOperateTable, err error) {
	datas, err := UserOperateTables(qs, DefaultOther)
	if err != nil {
		return
	}
	if len(datas) == 0 {
		err = NotFound
		return
	}
	data = datas[0]
	return
}
